package com.cljz.peipei.base;

import android.os.Handler;
import android.os.Message;

import com.cljz.peipei.utils.T;
import com.cljz.peipei.widget.LoadingDialog;
import com.umeng.analytics.MobclickAgent;

import java.lang.ref.WeakReference;

import cn.droidlover.xdroidmvp.mvp.IPresent;
import cn.droidlover.xdroidmvp.mvp.XFragment;

/**
 * Created by apple on 2017/3/31.
 */

public abstract class BaseFragment<P extends IPresent> extends XFragment<P> {
    private static final int DISMISS_DIALOG = 233;
    private LoadingDialog loadingDialog;
    final Handler dialogHandler = new DialogHandler(this);

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(context);

    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(context);

    }

    private static class DialogHandler extends Handler {
        private final WeakReference<BaseFragment> mFragment;

        DialogHandler(BaseFragment fragment) {
            mFragment = new WeakReference<>(fragment);
        }

        @Override
        public void handleMessage(Message msg) {
            BaseFragment fragment = mFragment.get();
            switch (msg.what) {
                case DISMISS_DIALOG:
                    fragment.dismissLoadingDialog();
                    T.getInstance().showShort("网络超时");
                    break;
            }
        }
    }

    public void showLoadingDialog() {
        loadingDialog = new LoadingDialog(App.getInstance());
        loadingDialog.show();
        dialogHandler.sendEmptyMessageDelayed(DISMISS_DIALOG, 10000);
    }

    public void dismissLoadingDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }
}
